//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Globals.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
THexForm *HexForm;
//---------------------------------------------------------------------------
__fastcall THexForm::THexForm(TComponent* Owner)
 : TForm(Owner)
{
}

//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::DrawHex( BYTE *String )
{
  WORD x,
       y,
       ptr,
       fnHeight;
  char buf[16];

  if ( !rom ) return;

  memset( hxData, 0, 0x1000 );

  // GET the BYTE count
  hexx.posi = 0;
  count     = 0;
  while( count < 0x1000 && String[count] != 0xFF )
  { //
    hxData[ count ] = String[ count ];
    count++;
    if ( String[ count ] == 0xFF ){
      hxData[ count ] = String[ count ];
      count++;
      break;
    }
  }

  //
  if ( !count && String[ count ] == 0xFF ){
    hxData[ count ] = String[ count ];
    count++;
  }

  //
  fnHeight = abs( HexForm->Font->Height ) + 2;
  //
  hexx.mx = 16;
  hexx.my = (HexForm->ClientHeight / fnHeight);
  // Re-adjust the window height
//  HexForm->ClientHeight = (hexx.my * fnHeight);
  //
  y   = -1;
  while( ++y < hexx.my )
  { //
    ptr = 0;
    x = -1;
    while( ++x < hexx.mx )
    { //
      sprintf( buf, "%02X", hxData[ (y << 4) + x ] );
      //
      if ( x == hexx.x && y == hexx.y )
      { //
        HexForm->Canvas->Font->Color = clRed;
        SetBkColor( HexForm->Canvas->Handle, clWhite);
      }
      else
      { //
        HexForm->Canvas->Font->Color = ( x & 1 ) ? clWhite : clTeal;
        SetBkColor( HexForm->Canvas->Handle, clBlack );
      }

      //
			 HexForm->Canvas->TextOut( ptr, (y * fnHeight), buf );
      ptr += HexForm->Canvas->TextWidth( AnsiString(buf) );
    }
  }
  //
  DrawPic( );
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::UpdateHex( BYTE *String )
{
  WORD x,
       y,
       ptr,
       fnHeight;
  char buf[16];

  if ( !rom ) return;
  //
  fnHeight = abs( HexForm->Font->Height ) + 2;
  //
  hexx.mx = 16;
  hexx.my = (HexForm->ClientHeight / fnHeight);
  // Re-adjust the window height
//  HexForm->ClientHeight = (hexx.my * fnHeight);
  //
  y   = -1;
  while( ++y < hexx.my )
  { //
    ptr = 0;
    x = -1;
    while( ++x < hexx.mx )
    { //
      sprintf( buf, "%02X", hxData[ hexx.posi + (y << 4) + x ] );
      //
      if ( x == hexx.x && y == hexx.y )
      { //
        HexForm->Canvas->Font->Color = clRed;
        SetBkColor( HexForm->Canvas->Handle, clWhite);
      }
      else
      { //
        HexForm->Canvas->Font->Color = ( x & 1 ) ? clWhite : clTeal;
        SetBkColor( HexForm->Canvas->Handle, clBlack );
      }

      //
			 HexForm->Canvas->TextOut( ptr, (y * fnHeight), buf );
      ptr += HexForm->Canvas->TextWidth( AnsiString(buf) );
    }
  }
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::FormCreate(TObject *Sender)
{
  hexx.mx = 0;
  hexx.my = 0;
  hexx.x  = 0;
  hexx.y  = 0;

  PIC = new Graphics::TBitmap;

  PIC->Width       = 16;
  PIC->Height      = 16;
  PIC->PixelFormat = pf16bit;
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
  switch( Key )
  {
    case VK_LEFT : if ( hexx.x > 0 ) hexx.x--; else { hexx.x = (hexx.mx-1); goto _Up; }   break;
    case VK_RIGHT: if ( hexx.x < (hexx.mx-1) ) hexx.x++; else { hexx.x = 0; goto _Down; } break;
    //------------
    case VK_UP   : _Up:
                    if ( hexx.y > 0 ){ hexx.y--; break; }
                    if ( !hexx.y  && hexx.posi ) hexx.posi -= hexx.mx;
                    break;
    //------------
    case VK_DOWN : _Down:
                  if ( hexx.y < (hexx.my-1) ){ hexx.y++; break; }
                  if ( hexx.y == (hexx.my-1) && (hexx.posi + hexx.mx) < 0x1000 ) hexx.posi += hexx.mx;
                  break;
    //----------------------
    default : return;
  }
  //
  UpdateHex( hxData );
  DrawPic( );
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::DrawPic( )
{
  LONG posi;
  BYTE TILE[16][16];
  WORD *VRAM;
  BYTE BUFR[0x20],
       flag = 0;

  if ( !rom ) return;
/*
  posi = hxData[ hexx.posi + (hexx.y << 4) + hexx.x];
  memset( (BYTE *)TILE, 0, 256 );
  //
  if ( posi >= 0x21 && (posi != 0xFF && posi != 0xFE) )
    posi = *(WORD *)&hxData[ hexx.posi + (hexx.y << 4) + hexx.x];

  // Adjustments
  if ( posi < 0x0100 )
    posi--;
  else
  if ( posi >= 0x0100 && posi < 0x0200 )
    posi -= 3;
  else
  if ( posi >= 0x0200 && posi < 0x0300 )
    posi -= 5;
  else
  if ( posi >= 0x0300 && posi < 0x0400 )
    posi -= 7;
  else
    posi -= 9;

  //----------------//
  // FONT is 16x16  /
  //----------------//
  posi  = (posi << 5) ;
  memcpy( BUFR, &rom[ Snes2Pc(0x288000 + posi) ], 0x20 );
  // Convert the tile data
  int x = -1;
  while( ++x < 16 )
    Dec2Bin( TILE[ x ], (BUFR[ x ] << 8) + BUFR[ x + 16 ], 16 );

  //---------------//
  // DMA to Vram   //
  //---------------//
  for ( int y = 0; y < 16; y++ )
  { //
    (void *)VRAM = PIC->ScanLine[ y ];
    for ( int x = 0; x < 16; x++ )
      VRAM[ x ] = DOS565[ TILE[ y ][ x ] * 0x0F ];
  }
  //
  HexForm->Canvas->StretchDraw( Rect( 264, 4, 390, 128 ), PIC );
  //
  // Display the Offset
  if ( (hexx.posi + (hexx.y << 3) + hexx.x) < (count-1) )
    HexForm->Caption = "Hex Data - $" + IntToHex( ( (0x7E0000 | txt.pntr) + ((hexx.posi + (hexx.y << 3) + hexx.x)), 8 );
  else
    HexForm->Caption = "Hex Data - $" + IntToHex( ( (0x7E0000 | txt.pntr) + (count-1), 8 );
*/    
}

//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::FormPaint(TObject *Sender)
{
  UpdateHex( hxData );
  DrawPic( );
}


